# 16. 城市聚集度
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
class UnionFindSet {
constructor(n) {
this.father = new Array(n);
for(let i=0; i<n; i++) {
this.father[i] = i;
}
}
find(x) {
if (this.father[x] !=== x) {
this.father[x] = this.find(this.father[x]);
}
return this.father[x];
}
unionSet(x, y) {
const x_fa = this.find(x);
const y_fa = this.find(y);
if (x_fa !== y_fa) {
this.father[y_fa] = x_fa;
}
}
}
let n;
let relations = [];
const lines = [];
rl.on('line', (line) => {
if (!n) {
n = parseInt(line);
} else {
const [x, y] = line.split(' ').map(Number);
relations.push([x, y]);
}
});
rl.on('close', () => {
let min_dp = Infinity;
let city = [];
for(let i=1; i<=n; i++) {
const ufs = new UnionFindSet(n+1);
for(const [x,y] of relations) {
if (x===i || y===i)continue;
ufs.unionSet(x, y);
}
const count = new Map();
for(let f of ufs.father) {
f = ufs.find(f);
count.set(f, (count.get(f) || 0) + 1);
}
let dp = 0;
for(const c of count.values()) {
dp = Math.max(dp, c);
}
if (dp < min_dp) {
min_dp = dp;
city = [i];
} else if (dp === min_dp) {
city.push(i);
}
}
console.log(city.join(' '));
})
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71